package com.hqj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqj.dao.UserMapper;
import com.hqj.entity.User;
import com.hqj.service.UserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/10/7/007
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Value("${spring.mail.username}")
    private String emails;
    @Resource
    JavaMailSenderImpl javaMailSender;

    @Override
    public String registers(String username, String password, String email, String code) {
        //密码加密
        String salt=UUID.randomUUID().toString();
        SimpleHash sh=new SimpleHash(
                "MD5",//algorithmName算法名称
                password, //source要加密的密码
                salt, //加密盐
                1);//加密次数
        String pwd=sh.toHex();

        User user = new User();
        user.setPassword(pwd);
        user.setUsername(username);
        user.setEmail(email);
        user.setSalt(salt);
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return "注册成功";
        }
        return "注册失败";
    }

    /**
     * 注销
     *
     * @param username
     * @return java.lang.String
     * @date 2020/10/9 22:23
     */
    @Override
    public String logout(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("cancel", 0).eq("username", username);
        User user = userMapper.selectOne(wrapper);
        user.setCancel(1);
        int byId = userMapper.updateById(user);
        if (byId > 0) {
            return "已注销";
        }
        return "注销失败";
    }


    @Override
    public String transmit(String email) {
        SimpleMailMessage message = new SimpleMailMessage();
//        主题
        message.setSubject("欢迎注册南风后台管理");
        //        生成验证码
        String cod = UUID.randomUUID().toString().substring(0, 4);
//        发送内容此时为验证码
        message.setText("验证码:" + cod);
//        注册的邮箱
        message.setTo(email);
//        发件人人--发送去哪里
        message.setFrom(emails);
//        开始发送
        javaMailSender.send(message);
        return cod;
    }

    @Override
    public List<User> selectAll() {

        return userMapper.selectAll();
    }


    @Override
    public User judgelogin(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 搜索用户
     *
     * @param name
     * @return
     */
    @Override
    public User search(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("cancel", 0).like("username", name);

        return userMapper.selectOne(wrapper);
    }

    /**
     * 修改账户
     *
     * @param id
     * @param username
     * @param email
     * @param name
     * @return com.hqj.entity.User
     * @date 2020/10/10 15:54
     */
    @Override
    public String modifyUser(long id, String username, String email, String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("cancel", 0).eq("id",id);
        User user = userMapper.selectOne(wrapper);
        user.setUsername(username);
        user.setEmail(email);
        user.setName(name);
        int update = userMapper.updateById(user);
        if (update > 0) {
            return "修改成功";
        }
        return "修改失败";
    }
/**
 *删除用户
 * @date 2020/10/11 14:59
 * @param id
 * @return java.lang.String
 */
    @Override
    public String deleteUser(long id) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("cancel",0).eq("id",id);
        int delete = userMapper.delete(wrapper);
        if (delete>0){
            return "已删除" ;
        }
        return"未删除" ;
    }
/**
 * 分页查询
 * @date 2020/10/11 15:01
 * @param page
 * @param size
 * @return java.util.List<com.hqj.entity.User>
 */
    @Override
    public List<User> findPage(int page, int size) {
        Page<User> userPage = userMapper.selectPage(new Page<>(page, size), null);
        return userPage.getRecords();
    }


}
